// Puppeteer任务
const PuppeteerScreenshot = require('./puppeteer/screenshot');
const PuppeteerContent = require('./puppeteer/content');
const PuppeteerForm = require('./puppeteer/form');
const PuppeteerClick = require('./puppeteer/click');
const PuppeteerDoubao = require('./puppeteer/doubao');

// Playwright任务
const PlaywrightScreenshot = require('./playwright/screenshot');
const PlaywrightContent = require('./playwright/content');
const PlaywrightForm = require('./playwright/form');
const PlaywrightClick = require('./playwright/click');

class Tasks {
  constructor(engine = 'puppeteer') {
    this.engine = engine.toLowerCase();
    this.validateEngine();
  }

  validateEngine() {
    if (!['puppeteer', 'playwright'].includes(this.engine)) {
      throw new Error('不支持的引擎类型，请选择 puppeteer 或 playwright');
    }
  }

  // 截图功能
  async screenshot(url, options = {}) {
    if (this.engine === 'puppeteer') {
      return PuppeteerScreenshot.takeScreenshot(url, options);
    } else {
      return PlaywrightScreenshot.takeScreenshot(url, options);
    }
  }

  // 获取页面内容
  async getContent(url, options = {}) {
    if (this.engine === 'puppeteer') {
      return PuppeteerContent.getContent(url, options);
    } else {
      return PlaywrightContent.getContent(url, options);
    }
  }

  // 填写表单
  async fillForm(url, formData, options = {}) {
    if (this.engine === 'puppeteer') {
      return PuppeteerForm.fillForm(url, formData, options);
    } else {
      return PlaywrightForm.fillForm(url, formData, options);
    }
  }

  // 点击元素
  async click(url, selector, options = {}) {
    if (this.engine === 'puppeteer') {
      return PuppeteerClick.click(url, selector, options);
    } else {
      return PlaywrightClick.click(url, selector, options);
    }
  }

  // 豆包图片生成（仅Puppeteer支持）
  async generateDoubaoImages(prompt, options = {}) {
    if (this.engine === 'puppeteer') {
      return PuppeteerDoubao.generateImages(prompt, options);
    } else {
      throw new Error('豆包图片生成功能仅支持Puppeteer引擎');
    }
  }

  // 获取当前引擎
  getEngine() {
    return this.engine;
  }

  // 切换引擎
  setEngine(engine) {
    this.engine = engine.toLowerCase();
    this.validateEngine();
  }
}

// 创建默认实例
const defaultTasks = new Tasks('puppeteer');

// 导出类和默认实例
module.exports = {
  Tasks,
  default: defaultTasks,
  
  // 便捷方法
  screenshot: (url, options) => defaultTasks.screenshot(url, options),
  getContent: (url, options) => defaultTasks.getContent(url, options),
  fillForm: (url, formData, options) => defaultTasks.fillForm(url, formData, options),
  click: (url, selector, options) => defaultTasks.click(url, selector, options),
  generateDoubaoImages: (prompt, options) => defaultTasks.generateDoubaoImages(prompt, options)
};
